package com.sy.easycms.bo;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;

import com.sy.easycms.Config;
import com.sy.easycms.dao.mysql.DAO;
import com.sy.easycms.orm.Content;

/**
 * Content表业务类
 * @author Luke Sun
 *
 */
public class ContentBO extends BaseBO {

	private DAO dao = new DAO();
	
	public ContentBO() {
	}

	private ArrayList<String> makeIntArray(int i)
	{
		ArrayList<String> al = new ArrayList<String>();
		if(i / Config.record < 1)
		{
			al.add("1");
			return al;
		}else
		{
			for(int j = 1;j<i%Config.record;j++)
			{
				al.add(j+"");
			}
			return al;
		}
	}
	
	public void postArticle(Content instance) throws SQLException
	{
		getSession().save(instance);
//		String sql = "insert into content values(uuid(),'"+instance.getTitle()+"','"+instance.getContent()+"','"+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(instance.getPostdate().getTime())+"','"+instance.getTag()+"')";
		
	}
	/**
	 * 分页查询
	 * @param page
	 * @return
	 * @throws SQLException
	 */
	public List<Content> selectArticleByPage(int page) throws SQLException
	{
//		int start = page * Config.record; 
//		List<Article> result = new ArrayList<Article>();
//		ResultSet rs = null;
//		rs = dao.select("select * from content limit "+start+","+Config.record);
//		if(rs != null)
//		{
//			while(rs.next())
//			{
//				Article chapter = new Article();
//				
//				chapter.setId(rs.getString("Id"));
//				chapter.setTitle(rs.getString("Title"));
//				chapter.setContent(rs.getString("Content"));
//				Calendar cal = Calendar.getInstance();
//				cal.setTime(rs.getDate("PostDate"));
//				chapter.setPostdate(cal);
//				chapter.setTag(rs.getString("Tag"));
//				result.add(chapter);
//			}
//		}
//		return result;
		Session session = getSession();
		Query query = session.createQuery("from Content");
		query.setMaxResults(Config.record);
		query.setFirstResult((page - 1)*Config.record);
		return query.list();
	}
	/**
	 * 全部页码
	 * @return
	 * @throws SQLException
	 */
	public List<String> getPage() throws SQLException
	{
		ResultSet rs = dao.select("select count(Id) from content;");
		if(rs.next())
		{
			//return makeIntArray(rs.getInt(0));
		}
		return null;
	}
}
